package org.csu.mypetstore.Controller;

import org.csu.mypetstore.domain.*;
import org.csu.mypetstore.service.AccountService;
import org.csu.mypetstore.service.CategortService;
import org.csu.mypetstore.service.ManagerService;
import org.csu.mypetstore.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("/Manager")
public class ManagerController {
        @Autowired
        ManagerService managerService;
        @Autowired
        AccountService accountService;
        @Autowired
        OrderService orderService;
        @Autowired
        CategortService categortService;

        @RequestMapping("/viewmainpage")
        public String viewmainpage(HttpServletRequest request, Model model){
                HttpSession session = request.getSession();
                String username = request.getParameter("username");
                String password = request.getParameter("password");
                if(managerService.isexist(username,password)){
                        Manager manager = managerService.getaccountbyusername(username);
                        session.setAttribute("manager", manager);
                        model.addAttribute("manager", manager);
                        return "/Manager/Mstoreinfir";
                }else {
                        return "/Manager/loginin";
                }
        }
        @RequestMapping("/viewsiginon")
        public String viewsiginon(){
                return "/index";
        }
        @RequestMapping("/viewregistration")
        public String viewregistration(){
                return "/Account/registration";
        }
        @RequestMapping("/registration")
        public String registration(HttpServletRequest request){
                Manager manager = new Manager();
                manager.setFullname(request.getParameter("fullname"));
                manager.setAddress(request.getParameter("address"));
                manager.setEmail(request.getParameter("email"));
                manager.setCity(request.getParameter("city"));
                manager.setUsername(request.getParameter("username"));
                manager.setPassword(request.getParameter("password"));
                manager.setSex(request.getParameter("sex"));
                managerService.register(manager);
                return "/login";
        }
        @RequestMapping("/viewmanagerinfor")
        public String registration(){
                return "/Account/managerinfor";
        }
        @RequestMapping("/viewAccountInfor")
        public String viewAccountInfor(Model model,HttpServletRequest request){
                HttpSession session = request.getSession();
                Manager manager = (Manager) session.getAttribute("manager");
                List<Account> accountList = accountService.getallAccount();
                List<Account> accountList1 = accountService.getallAccountprofile();
                session.setAttribute("accountList",accountList);
                model.addAttribute("accountList",accountList);
                model.addAttribute("accountList1",accountList1);
                model.addAttribute("manager",manager);
                return "/Manager/UserinforManage";
        }
        @RequestMapping("/searchAccount")
        public String searchAccount(Model model,HttpServletRequest request){
                String keyword = request.getParameter("keyword");
                HttpSession session = request.getSession();
                Manager manager = (Manager) session.getAttribute("manager");
                List<Account> accountList = accountService.searchAccount(keyword);
                List<Account> accountList1 = accountService.searchAccountprofile(keyword);
                model.addAttribute("manager",manager);
                model.addAttribute("accountList",accountList);
                model.addAttribute("accountList1",accountList1);
                return "/Manager/UserinforManage";
        }
        @RequestMapping("/updateAccountPageOne")
        public String updateAccountPageOne(HttpServletRequest req,Model model){
                try {
                        req.setCharacterEncoding("UTF-8");
                } catch (Exception e) {
                }
                String username = req.getParameter("username");
                HttpSession session = req.getSession();
                Manager manager = (Manager) session.getAttribute("manager");
                Account account = accountService.getAccount(username);
                String password = req.getParameter("password");
                String firstName = req.getParameter("firstname");
                String lastName = req.getParameter("lastname");
                String email = req.getParameter("email");
                String city = req.getParameter("city");

                String country = req.getParameter("country");

                account.setUsername(username);
                account.setPassword(password);
                account.setFirstName(firstName);
                account.setLastName(lastName);
                account.setEmail(email);
                account.setCity(city);
                account.setCountry(country);
                accountService.updateAccount(account);
                model.addAttribute("manager",manager);

                List<Account> accountList = accountService.getallAccount();
                List<Account> accountList1 = accountService.getallAccountprofile();
                model.addAttribute("accountList",accountList);
                model.addAttribute("accountList1",accountList1);

                return "/Manager/UserinforManage";
        }
        @RequestMapping("/updateAccountPageTwo")
        public String updateAccountPageTwo(HttpServletRequest req,Model model){
                try {
                        req.setCharacterEncoding("UTF-8");
                } catch (Exception e) {
                }
                String username = req.getParameter("username");
                HttpSession session = req.getSession();
                Manager manager = (Manager) session.getAttribute("manager");
                Account account = accountService.getAccount(username);
                String address1 = req.getParameter("address1");
                String address2 = req.getParameter("address2");
                String status = req.getParameter("status");
                String state = req.getParameter("state");
                String zip = req.getParameter("zip");
                String phone = req.getParameter("phone");

                account.setAddress1(address1);
                account.setAddress2(address2);
                account.setStatus(status);
                account.setState(state);
                account.setZip(zip);
                account.setPhone(phone);
                accountService.updateAccount(account);
                model.addAttribute("manager",manager);

                List<Account> accountList = accountService.getallAccount();
                List<Account> accountList1 = accountService.getallAccountprofile();
                model.addAttribute("accountList",accountList);
                model.addAttribute("accountList1",accountList1);
                return "/Manager/UserinforManage";
        }
        @RequestMapping("/updateAccountprofile")
        public String updateAccountprofile(HttpServletRequest req,Model model){
                try {
                        req.setCharacterEncoding("UTF-8");
                } catch (Exception e) {
                }
                String username = req.getParameter("username");
                HttpSession session = req.getSession();
                Manager manager = (Manager) session.getAttribute("manager");
                Account account = accountService.getAccount(username);
                String languagePreference = req.getParameter("languagePreference");
                String favouriteCategoryId = req.getParameter("favouriteCategoryId");
                String listOption = req.getParameter("listOption");
                String bannerOption = req.getParameter("bannerOption");

                account.setLanguagePreference(languagePreference);
                account.setFavouriteCategoryId(favouriteCategoryId);
                account.setListOption(Boolean.parseBoolean(listOption));
                account.setBannerOption(Boolean.parseBoolean(bannerOption));
                accountService.updateAccount(account);

                model.addAttribute("manager",manager);

                List<Account> accountList = accountService.getallAccount();
                List<Account> accountList1 = accountService.getallAccountprofile();
                model.addAttribute("accountList",accountList);
                model.addAttribute("accountList1",accountList1);
                return "/Manager/UserinforManage";
        }
        @RequestMapping("/editAccount")
        public String editAccount(HttpServletRequest req,Model model){
                String username = req.getParameter("username");
                HttpSession session = req.getSession();
                Account account = accountService.getAccount(username);
                model.addAttribute("account",account);
                session.setAttribute("account",account);
                return "/Manager/Editaccount";
        }
        @RequestMapping("/deleteAccount")
        public String deleteAccount(HttpServletRequest req,Model model){
                String username = req.getParameter("username");
                accountService.deleteAccount(username);
                HttpSession session = req.getSession();
                Manager manager = (Manager) session.getAttribute("manager");
                model.addAttribute("manager",manager);
                List<Account> accountList = accountService.getallAccount();
                List<Account> accountList1 = accountService.getallAccountprofile();
                model.addAttribute("accountList",accountList);
                model.addAttribute("accountList1",accountList1);
                return "/Manager/UserinforManage";
        }
        @RequestMapping("/viewOrderInfor")
        public String viewOrderInfor(Model model ,HttpServletRequest request){
                HttpSession session = request.getSession();
                Manager manager = (Manager)session.getAttribute("manager");
                List<Order> orderList = orderService.getAllOrder();
                model.addAttribute("manager",manager);
                model.addAttribute("orderList",orderList);
                return "/Manager/OrderManage";
        }
        @RequestMapping("/deliveryorder")
        public String deliveryorder(Model model,HttpServletRequest request){
                String orderId = request.getParameter("orderid");
                String status = "ok";
                orderService.deliveryorder(orderId,status);
                HttpSession session = request.getSession();
                Manager manager = (Manager)session.getAttribute("manager");
                List<Order> orderList = orderService.getAllOrder();
                model.addAttribute("manager",manager);
                model.addAttribute("orderList",orderList);
                return "/Manager/OrderManage";
        }
        @RequestMapping("/deleteorder")
        public String deleteorder(Model model ,HttpServletRequest request){
                HttpSession session =request.getSession();
                String orderid = request.getParameter("orderid");
                orderService.deleteorder(orderid);
                Manager manager = (Manager)session.getAttribute("manager");
                List<Order> orderList = orderService.getAllOrder();
                model.addAttribute("manager",manager);
                model.addAttribute("orderList",orderList);
                return "/Manager/OrderManage";

        }
        @RequestMapping("/searchorder")
        public String searchorder(Model model ,HttpServletRequest request){
                HttpSession session = request.getSession();
                String keyword = request.getParameter("keyword");
                List<Order> orderList = orderService.searchorder(keyword);
                Manager manager = (Manager)session.getAttribute("manager");
                model.addAttribute("manager",manager);
                model.addAttribute("orderList",orderList);
                return "/Manager/OrderManage";
        }
        //category操作
        @RequestMapping("/viewCategory")
        public String viewCategory(Model model ,HttpServletRequest request){
                HttpSession session = request.getSession();
                Manager manager = (Manager)session.getAttribute("manager");
                model.addAttribute("manager",manager);
                List<Category> categoryList = categortService.getCategoryList();
                model.addAttribute("categoryList",categoryList);
                return "/Manager/CategoryManage";
        }
        @RequestMapping("/deleteCategory")
        public String deleteCategory(HttpServletRequest req, Model model){
                String ItemId = req.getParameter("ItemId");
                HttpSession session = req.getSession();
                Manager manager = (Manager)session.getAttribute("manager");
                model.addAttribute("manager",manager);
                //删除Category后应该删除对应的product和对应的Item
                List<Product> productList = categortService.getallproduct();
                List<Item> itemList = categortService.getallItem();
                for(int j=0;j<productList.size();j++){
                        if(productList.get(j).getCategoryId().equals(ItemId)){
                        for(int i=0;i<itemList.size();i++){
                                if(itemList.get(i).getProductId().equals(productList.get(j).getProductId())){
                                        categortService.deleteItem(itemList.get(i).getItemId());
                                        categortService.deletelatestproduct(itemList.get(i).getItemId());
                                }
                        }
                        categortService.deleteProduct(productList.get(j).getProductId());
                }
                }
                categortService.deleteCastegory(ItemId);
                List<Category> categoryList = categortService.getCategoryList();
                model.addAttribute("categoryList",categoryList);
                return "/Manager/CategoryManage";
        }
        @RequestMapping("/insertCategory")
        public String insertCategory( Model model, HttpServletRequest request,@RequestParam(value = "file") MultipartFile file){
                String categoryId = request.getParameter("categoryId");
                String name = request.getParameter("name");
                String description = request.getParameter("description");
                if (file.isEmpty()) {
                        System.out.println("文件为空");
                }
                String fileName = file.getOriginalFilename();  // 文件名
                if(fileName.indexOf("\\") != -1){
                        fileName = fileName.substring(fileName.lastIndexOf("\\"));
                }
                String filePath = System.getProperty("user.dir")+"\\src\\main\\resources\\static\\images\\"; // 上传后的路径
                String path = "../images/"+fileName;
                File dest = new File(filePath + fileName);
                if (!dest.getParentFile().exists()) {
                        dest.getParentFile().mkdirs();
                }
                try {
                        file.transferTo(dest);
                }catch (Exception e){

                }
                //设置description
                description = "<"+"image"+" src"+"="+"\""+path+"\""+">"+description;
                HttpSession session = request.getSession();
                Manager manager = (Manager)session.getAttribute("manager");
                model.addAttribute("manager",manager);
                List<Category> categoryList = categortService.getCategoryList();
                boolean flag= true;
                for(int i=0;i<categoryList.size();i++){
                        if(categoryList.get(i).getCategoryId().equals(categoryId)){
                                flag = false;
                                break;
                        }
                }
                if(flag){
                        Category category = new Category();
                        category.setCategoryId(categoryId);
                        category.setName(name);
                        category.setDescription(description);
                        categortService.insertCategory(category);
                }
                categoryList = categortService.getCategoryList();
                model.addAttribute("categoryList",categoryList);
                return "/Manager/CategoryManage";
        }
        @RequestMapping("/viewnewCategory")
        public String viewnewCategory(){
                return "/Manager/newCategory";
        }
        @RequestMapping("/searchCategory")
        public String searchCategory(HttpServletRequest req, Model model){
                HttpSession session = req.getSession();
                Manager manager = (Manager)session.getAttribute("manager");
                model.addAttribute("manager",manager);
                String keyword = req.getParameter("keyword");
                List<Category> categoryList = categortService.searchCategory(keyword);
                model.addAttribute("categoryList",categoryList);
                return "/Manager/CategoryManage";
        }
        @RequestMapping("/updateCategory")
        public String updateCategory(HttpServletRequest req, Model model){
                String categoryId = req.getParameter("categoryId");
                String name = req.getParameter("name");
                String description = req.getParameter("description");
                HttpSession session = req.getSession();
                Manager manager = (Manager)session.getAttribute("manager");
                model.addAttribute("manager",manager);
                        Category category = new Category();
                        category.setCategoryId(categoryId);
                        category.setName(name);
                        category.setDescription(description);
                        categortService.updateCategory(category);
                List<Category> categoryList = categortService.getCategoryList();
                model.addAttribute("categoryList",categoryList);
                return "/Manager/CategoryManage";
        }
        @RequestMapping("/viewupdateCategory")
        public String viewupdateCategory(HttpServletRequest req, Model model){
                String categoryId = req.getParameter("categoryId");
                Category category =categortService.getCategory(categoryId);
                model.addAttribute("category",category);
                return "/Manager/UpdateCategory";
        }
        //product操作
        @RequestMapping("/viewProduct")
        public String viewProduct(Model model ,HttpServletRequest request){
                HttpSession session = request.getSession();
                Manager manager = (Manager)session.getAttribute("manager");
                model.addAttribute("manager",manager);
                List<Product> productList = categortService.getallproduct();
                model.addAttribute("productList",productList);
                return "/Manager/ProductManage";
        }
        @RequestMapping("/deleteProduct")
        public String deleteProduct(Model model ,HttpServletRequest req){
                String productId = req.getParameter("productId");
                HttpSession session = req.getSession();
                Manager manager = (Manager)session.getAttribute("manager");
                model.addAttribute("manager",manager);
                List<Item> itemList = categortService.getallItem();
                //删除Product后应该删除对应的Item
                for(int i=0;i<itemList.size();i++){
                        if(itemList.get(i).getProductId().equals(productId)){
                                categortService.deleteItem(itemList.get(i).getItemId());
                                categortService.deletelatestproduct(itemList.get(i).getItemId());
                        }
                }
                categortService.deleteProduct(productId);
                List<Product> productList = categortService.getallproduct();
                model.addAttribute("productList",productList);
                return "/Manager/ProductManage";
        }
        @RequestMapping("/insertProduct")
        public String insertProduct(Model model ,HttpServletRequest req,@RequestParam(value = "file") MultipartFile file){
                String productId = req.getParameter("productId");
                String category = req.getParameter("category");
                String name = req.getParameter("name");
                String descn = req.getParameter("descn");

                if (file.isEmpty()) {
                        System.out.println("文件为空");
                }
                String fileName = file.getOriginalFilename();  // 文件名
                if(fileName.indexOf("\\") != -1){
                        fileName = fileName.substring(fileName.lastIndexOf("\\"));
                }
                String filePath = System.getProperty("user.dir")+"\\src\\main\\resources\\static\\images\\"; // 上传后的路径
                String path = "../images/"+fileName;
                File dest = new File(filePath + fileName);
                if (!dest.getParentFile().exists()) {
                        dest.getParentFile().mkdirs();
                }
                try {
                        file.transferTo(dest);
                }catch (Exception e){

                }
                //设置description
                descn = "<"+"image"+" src"+"="+"\""+path+"\""+">"+descn;


                Product product = new Product();
                product.setProductId(productId);
                product.setCategory(category);
                product.setDescn(descn);
                product.setName(name);

                HttpSession session = req.getSession();
                Manager manager = (Manager)session.getAttribute("manager");
                model.addAttribute("manager",manager);
                categortService.insertProduct(product);
                List<Product> productList = categortService.getallproduct();
                model.addAttribute("productList",productList);
                return "/Manager/ProductManage";
        }
        @RequestMapping("/viewnewProduct")
        public String viewnewProduct(){
                return "/Manager/newProduct";
        }
        @RequestMapping("/searchProduct")
        public String searchProduct(Model model ,HttpServletRequest req){
                HttpSession session = req.getSession();
                Manager manager = (Manager)session.getAttribute("manager");
                model.addAttribute("manager",manager);
                String keyword = req.getParameter("keyword");
                List<Product> productList = categortService.searchProduct(keyword);
                model.addAttribute("productList",productList);
                return "/Manager/ProductManage";
        }
        @RequestMapping("/updateProdcut")
        public String updateProdcut(Model model ,HttpServletRequest req){
                String productId = req.getParameter("productId");
                String category = req.getParameter("category");
                String name = req.getParameter("name");
                String descn = req.getParameter("descn");

                Product product = new Product();
                product.setProductId(productId);
                product.setCategory(category);
                product.setDescn(descn);
                product.setName(name);

                HttpSession session = req.getSession();
                Manager manager = (Manager)session.getAttribute("manager");
                model.addAttribute("manager",manager);
                categortService.updateProdcut(product);
                List<Product> productList = categortService.getallproduct();
                model.addAttribute("productList",productList);
                return "/Manager/ProductManage";
        }
        @RequestMapping("/viewupdateProdcut")
        public String viewupdateProdcut(Model model,HttpServletRequest req){
                String productId = req.getParameter("productId");
                Product product =categortService.getProductByProductID(productId);
                model.addAttribute("product",product);
                return "/Manager/UpdateProduct";
        }
        //Item操作
        @RequestMapping("/viewItem")
        public String viewItem(Model model ,HttpServletRequest req){
                HttpSession session = req.getSession();
                Manager manager = (Manager)session.getAttribute("manager");
                model.addAttribute("manager",manager);
                List<Item> itemList = categortService.getallItem();
                for(int i=0;i<itemList.size();i++){
                        itemList.get(i).setInstock(categortService.selectinstock(itemList.get(i).getItemId()));
                }
                model.addAttribute("itemList",itemList);
                return "/Manager/ItemManage";
        }
        @RequestMapping("/deleteItem")
        public String deleteItem(Model model ,HttpServletRequest req){
                HttpSession session = req.getSession();
                Manager manager = (Manager)session.getAttribute("manager");
                model.addAttribute("manager",manager);
                String ItemId = req.getParameter("ItemId");
                categortService.deleteItem(ItemId);
                categortService.deletelatestproduct(ItemId);
                List<Item> itemList = categortService.getallItem();
                for(int i=0;i<itemList.size();i++){
                        itemList.get(i).setInstock(categortService.selectinstock(itemList.get(i).getItemId()));
                }
                model.addAttribute("itemList",itemList);
                return "/Manager/ItemManage";
        }
        @RequestMapping("/insertItem")
        public String insertItem(Model model ,HttpServletRequest req){
                String itemId = req.getParameter("itemId");
                String listPrice = req.getParameter("listPrice");
                String productId = req.getParameter("productId");
                String unitcost = req.getParameter("unitcost");
                String supplier = req.getParameter("supplier");
                String status = req.getParameter("status");
                String attribute1 = req.getParameter("attribute1");
                int quantity = Integer.valueOf(req.getParameter("quantity"));

                Item item = new Item();
                item.setItemId(itemId);
                item.setProductId(productId);
                BigDecimal listPrice1=new BigDecimal(listPrice);
                item.setListPrice(listPrice1);
                BigDecimal unitcost1=new BigDecimal(unitcost);
                item.setUnitcost(unitcost1);
                item.setSupplier(Integer.valueOf(supplier));
                item.setStatus(status);
                item.setAttribute1(attribute1);
                item.setQuantity(quantity);

                categortService.inserItem(item);
                LatestProduct latestProduct = new LatestProduct();
                latestProduct.setItemid(item.getItemId());
                latestProduct.setDescn(categortService.getProduct(itemId).getDescn());
                latestProduct.setListprice(item.getListPrice());
                categortService.insertLatestProduct(latestProduct);
                List<Item> itemList = categortService.getallItem();
                for(int i=0;i<itemList.size();i++){
                        itemList.get(i).setInstock(categortService.selectinstock(itemList.get(i).getItemId()));
                }
                model.addAttribute("itemList",itemList);
                HttpSession session = req.getSession();
                Manager manager = (Manager)session.getAttribute("manager");
                model.addAttribute("manager",manager);
                return "/Manager/ItemManage";
        }
        /*@RequestMapping("/insertItem")
        public String insertItem(Model model ,HttpServletRequest req){
                String itemId = req.getParameter("itemId");
                String listPrice = req.getParameter("listPrice");
                String productId = req.getParameter("productId");
                String unitcost = req.getParameter("unitcost");
                String supplier = req.getParameter("supplier");
                String status = req.getParameter("status");
                String attribute1 = req.getParameter("attribute1");
                int quantity = Integer.parseInt(req.getParameter("quantity"));
                BigDecimal listPrice1 = new BigDecimal(listPrice);
                BigDecimal unitcost1 = new BigDecimal(unitcost);

                Item item = new Item();
                item.setItemId(itemId);
                item.setProductId(productId);
                item.setListPrice(listPrice1);
                item.setUnitcost(unitcost1);
                item.setSupplier(Integer.valueOf(supplier));
                item.setStatus(status);
                item.setAttribute1(attribute1);
                item.setQuantity(quantity);

                LatestProduct latestProduct = new LatestProduct();
                latestProduct.setItemid(itemId);
                latestProduct.setListprice(listPrice1);
                latestProduct.setDescn(categortService.getProduct(itemId).getDescn());
                categortService.insertLatestProduct(latestProduct);
                categortService.inserItem(item);
                List<Item> itemList = categortService.getallItem();
                for(int i=0;i<itemList.size();i++){
                        itemList.get(i).setInstock(categortService.selectinstock(itemList.get(i).getItemId()));
                }
                model.addAttribute("itemList",itemList);
                HttpSession session = req.getSession();
                Manager manager = (Manager)session.getAttribute("manager");
                model.addAttribute("manager",manager);
                return "/Manager/ItemManage";
        }*/
        @RequestMapping("/viewnewItem")
        public String viewnewItem(){
                return "/Manager/newItem";
        }

        @RequestMapping("/searchItem")
        public String searchItem(Model model ,HttpServletRequest req){
                HttpSession session = req.getSession();
                Manager manager = (Manager)session.getAttribute("manager");
                model.addAttribute("manager",manager);
                String keyword = req.getParameter("keyword");
                List<Item> itemList = categortService.searchItem(keyword);
                model.addAttribute("itemList",itemList);
                for(int i=0;i<itemList.size();i++){
                        itemList.get(i).setInstock(categortService.selectinstock(itemList.get(i).getItemId()));
                }
                return "/Manager/ItemManage";
        }
        @RequestMapping("/updateItem")
        public String updateItem(Model model ,HttpServletRequest req){
                String itemId = req.getParameter("itemId");
                String listPrice = req.getParameter("listPrice");
                String productId = req.getParameter("productId");
                String unitcost = req.getParameter("unitcost");
                String supplier = req.getParameter("supplier");
                String status = req.getParameter("status");
                String attribute1 = req.getParameter("attribute1");
                int quantity = Integer.parseInt(req.getParameter("quantity"));
                BigDecimal listPrice1 = new BigDecimal(listPrice);
                BigDecimal unitcost1 = new BigDecimal(unitcost);


                Item item = new Item();
                item.setItemId(itemId);
                item.setProductId(productId);
                item.setListPrice(listPrice1);
                item.setUnitcost(unitcost1);
                item.setSupplier(Integer.valueOf(supplier));
                item.setStatus(status);
                item.setAttribute1(attribute1);
                item.setQuantity(quantity);

                categortService.updateItem(item);
                List<Item> itemList = categortService.getallItem();
                for(int i=0;i<itemList.size();i++){
                        itemList.get(i).setInstock(categortService.selectinstock(itemList.get(i).getItemId()));
                }
                model.addAttribute("itemList",itemList);
                HttpSession session = req.getSession();
                Manager manager = (Manager)session.getAttribute("manager");
                model.addAttribute("manager",manager);
                return "/Manager/ItemManage";
        }
        @RequestMapping("/viewupdateItem")
        public String viewupdateItem(Model model,HttpServletRequest req){
                String ItemId = req.getParameter("ItemId");
                Item Item =categortService.getItem(ItemId);
                int quantity = categortService.getQuanatity(ItemId);
                Item.setQuantity(quantity);
                model.addAttribute("Item",Item);
                return "/Manager/UpdateItem";
        }
        @RequestMapping("/putoffItem")
        public String putoffItem(Model model,HttpServletRequest req){
                String ItemId = req.getParameter("ItemId");
                /*if(categortService.selectinstock(ItemId).equals("true")){
                        categortService.updateinstock(ItemId,"false");
                }
                else {
                        categortService.updateinstock(ItemId,"true");
                }*/
                categortService.updateinstock(ItemId,"false");
                List<Item> itemList= categortService.getallItem();
                for(int i=0;i<itemList.size();i++){
                        itemList.get(i).setInstock(categortService.selectinstock(itemList.get(i).getItemId()));
                }
                HttpSession session = req.getSession();
                Manager manager = (Manager)session.getAttribute("manager");
                model.addAttribute("manager",manager);
                model.addAttribute("itemList",itemList);
                return "/Manager/ItemManage";
        }

        @RequestMapping("/putonItem")
        public String putonItem(Model model,HttpServletRequest req){
                String ItemId = req.getParameter("ItemId");
                /*if(categortService.selectinstock(ItemId).equals("true")){
                        categortService.updateinstock(ItemId,"false");
                }
                else {
                        categortService.updateinstock(ItemId,"true");
                }*/
                categortService.updateinstock(ItemId,"true");
                List<Item> itemList= categortService.getallItem();
                for(int i=0;i<itemList.size();i++){
                        itemList.get(i).setInstock(categortService.selectinstock(itemList.get(i).getItemId()));
                }
                HttpSession session = req.getSession();
                Manager manager = (Manager)session.getAttribute("manager");
                model.addAttribute("manager",manager);
                model.addAttribute("itemList",itemList);
                return "/Manager/ItemManage";
        }

}
